package edu.upf.opencities.dao;

import java.util.List;

import javax.xml.datatype.XMLGregorianCalendar;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import twitter4j.Tweet;

import edu.upf.opencities.dao.connection.CassandraServiceConnector;
import edu.upf.opencities.dao.connection.TwitterConnector;
import edu.upf.opencities.datamodel.OSNEntry;
import edu.upf.opencities.datamodel.OSNService;

public class ServiceDAOImpl implements ServiceDAO {

	public static Logger logger = Logger.getLogger(ServiceDAOImpl.class);
	
    @Autowired
    private CassandraServiceConnector serviceConnector;
    @Autowired
    private TwitterConnector twitterConnector;
    
	@Override
	public String registerService(String mail, String serviceName,
			String servicePassword, String owner, String description,
			String updatePeriod, String section, String latitude,
			String longitude, List<String> tagList) {

		serviceConnector.connect();
		String idOSNService = serviceConnector.registerService(mail, serviceName, 
				servicePassword, owner, description, updatePeriod, 
				section, latitude, longitude, tagList);
		serviceConnector.disconnect();
		return idOSNService;
	}

	@Override
	public void deleteService(String idOSNService) {
		serviceConnector.connect();
		serviceConnector.deleteService(idOSNService);
		serviceConnector.disconnect();	
		
	}

	@Override
	public OSNService getService(String idOSNService) {
		serviceConnector.connect();
		OSNService service = serviceConnector.getService(idOSNService);
		serviceConnector.disconnect();	
		return service;
	}

	@Override
	public List<OSNService> getServicesByMail(String mail) {
		serviceConnector.connect();
		List<OSNService> listServices = serviceConnector.getServicesByMail(mail);
		serviceConnector.disconnect();	
		return listServices;
	}

	@Override
	public List<OSNService> getServicesBySection(String section) {
		serviceConnector.connect();
		List<OSNService> listServices = serviceConnector.getServicesBySection(section);
		serviceConnector.disconnect();	
		return listServices;
	}

	@Override
	public List<OSNService> getServicesByTag(String tag) {
		serviceConnector.connect();
		List<OSNService> listServices = serviceConnector.getServicesByTag(tag);
		serviceConnector.disconnect();	
		return listServices;
	}

	@Override
	public String addEntry(String idOSNService, XMLGregorianCalendar timeStamp,
			String value, String longitude, String latitude) {
		serviceConnector.connect();
		String idOSNEntry = serviceConnector.addEntry(
				idOSNService, timeStamp, value, longitude, latitude);
		//Now we create the tweet update message
		OSNService service = serviceConnector.getTwitter(idOSNService);
		if(service!=null){
			twitterConnector.updateStatus(
					service.getTwitterConsumerKey(), service.getTwitterConsumerSecret(), 
					service.getTwitterToken(), service.getTwitterTokenSecret (), 
					value);
			logger.info("Service has Twitter account");
		} else {
			logger.info("Service does not have Twitter account");
		}
		serviceConnector.disconnect();

		
		
		return idOSNEntry;
	}

	@Override
	public List<OSNEntry> getEntriesFromService(String idOSNService,
			int numEntries) {
		serviceConnector.connect();
		List<OSNEntry> listEntries = serviceConnector.getEntriesFromService(idOSNService, numEntries);
		serviceConnector.disconnect();	
		return listEntries;
	}

	@Override
	public OSNEntry getLastEntry(String idOSNService) {
		serviceConnector.connect();
		OSNEntry entry = serviceConnector.getLastEntry(idOSNService);
		serviceConnector.disconnect();	
		return entry;
	}

	@Override
	public void addTwitter(String idOSNService, String consumerKey,
			String consumerSecret, String token, String tokenSecret) {
		serviceConnector.connect();
		serviceConnector.addTwitter(idOSNService, consumerKey, consumerSecret, token, tokenSecret);
		serviceConnector.disconnect();	
	}

	@Override
	public boolean loginService(String idOSNService, String servicePassword) {
		// TODO Auto-generated method stub
		serviceConnector.connect();
		boolean isLogged = serviceConnector.loginService(idOSNService, servicePassword);
		serviceConnector.disconnect();	
		return isLogged;
	}
	

//	@Override
//	public OSNService getTwitter(String idOSNService) {
//		// TODO Auto-generated method stub
//		serviceConnector.connect();
//		OSNService service = serviceConnector.getTwitter(idOSNService);
//		serviceConnector.disconnect();	
//		return service;
//	}

}
